package com.gscitysfy.cus.drain.modules.command.convert;

import com.gscitysfy.cus.drain.modules.command.dto.InstructCarDto;
import com.gscitysfy.cus.drain.modules.command.dto.ResourceDispatchDto;
import com.gscitysfy.cus.drain.modules.command.entity.InstructCar;
import com.gscitysfy.cus.drain.modules.command.service.IInstructCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/24 19:04
 * @Description TODO
 */
@Component
public class InstructCarConvert {

    @Autowired
    @Lazy
    private IInstructCarService iInstructCarService;

    public InstructCar toEntity(InstructCar instructCar, InstructCarDto instructCarDto) {
        instructCar.setDangerSituationId(instructCarDto.getDangerSituationId());
        instructCar.setCarId(instructCarDto.getCarId());
        instructCar.setMessageTemplate(instructCarDto.getMessageTemplate());
        instructCar.setStatus(instructCarDto.getStatus());
        return instructCar;
    }

    public List<InstructCar> toEntityList(ResourceDispatchDto resourceDispatchDto) {
        List<InstructCar> instructCarList = new ArrayList<>();
        List<InstructCarDto> instructCarDtoList = resourceDispatchDto.getInstructCarDtoList();
        //过滤之前筛选的车辆
        Set<Long> selectedCarId = iInstructCarService.fetchByDangerSituation(resourceDispatchDto.getDangerSituationId())
                .stream()
                .map(InstructCar::getCarId)
                .collect(Collectors.toSet());
        List<InstructCarDto> filterInstructCarDtoList = instructCarDtoList
                .stream()
                .filter(c -> !selectedCarId.contains(c.getCarId()))
                .collect(Collectors.toList());

        InstructCar instructCar;
        for (InstructCarDto instructCarDto : filterInstructCarDtoList) {
            instructCar = new InstructCar();
            instructCarDto.setStatus("PENDING");
            instructCarDto.setDangerSituationId(resourceDispatchDto.getDangerSituationId());
            instructCarList.add(toEntity(instructCar, instructCarDto));
        }
        return instructCarList;
    }
}
